#! /bin/bash
# 写入文件内容
cat>/usr/local/etc/proc_monitor.sh<<EOF
#!/bin/sh
#############################
#info  : proc monitor
#create: 2020.07.19
#writer: yangxinjian
#copyer: sam
#update: 2023.04.13
#############################

source  /etc/profile

log_file=/root/vcom/proc_monitor.log
str_time_start=`date  +%Y%m%d%H%M%S`
echo  "----${str_time_start}----start proc--" >> ${log_file}

declare -A proc_dic
proc_dic[shared_moped_supervisor_java.jar]="/opt/gongxiangcity/start_java.sh"
proc_dic[shared_moped_supervisor_openapi.jar]="/opt/gongxiangcity/start_openapi.sh"
proc_dic[nginx]="nginx -c /etc/nginx/nginx.conf"

sleep_time="30"
log_file_max=`expr 10 \* 1024 \* 1024`
n=1
while true
do
        str_time_now=`date  +"%Y-%m-%d %H:%M:%S"`
        echo  "----n=[${n}]----${str_time_now}" >> ${log_file}

        log_size=`ls -l ${log_file} | awk '{print $5}'`
        if [[ ${log_size} -gt ${log_file_max} ]]; then
                > ${log_file}
                echo "log_size=[${log_size}]--max=[${log_file_max}]---->>log file size greater than max size, had del [${str_time_now}]!!!!" >> ${log_file}
        fi

        for key in ${!proc_dic[@]}
        do
                proc_name="$key"
                proc_start="${proc_dic[$key]}"
                proc_num=`ps -fe|grep "${proc_name}"|egrep  -v "grep" |wc -l`
                if [[ ${proc_num} -lt  1 ]]; then
                        echo "${str_time_now}----proc_name=[${proc_name}]----proc_num=[${proc_num}]----[${proc_start}]  not exit, need  start!!!" >> ${log_file}
                        "${proc_start}"   > /dev/null &
                        ps -ef|grep "${proc_name}"|egrep  -v "grep"  >> ${log_file}
                fi
        done

        sleep  ${sleep_time}
        let n+=1
done
EOF
# 给可执行权限
chmod 777 /usr/local/etc/proc_monitor.sh
